from fastapi import FastAPI
from contextlib import asynccontextmanager
from backend.mongo import connect_to_mongo, close_mongo_connection
from backend.api.router import api_router_v1


@asynccontextmanager
async def lifespan(app: FastAPI):
    # on startup
    await connect_to_mongo()
    yield
    # on shutdown
    await close_mongo_connection()


app = FastAPI(
    title="DWF AI",
    description="DWF AI平台后端服务",
    version="1.0.0",
    lifespan=lifespan
)


@app.get("/", tags=["Root"])
async def read_root():
    return {"message": "Welcome to DWF Instance Manager API"}


@app.get("/ping", tags=["Root"])
async def ping():
    return {"message": "ok"}


# register main router
app.include_router(api_router_v1, prefix="/api/v1")

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, port=9088)